﻿#pragma once
//用于使用 compress 制作的shader文件
//全部参数的语意Semantic 跟 命名都要一致
/*
预定义的 Semantic
Standard Software Semantics 
Common  Material  and  Light Characteristics 


POSITION 


DIRECTION 
DIFFUSE 
SPECULAR 
AMBIENT  
POWER 
SPECULARPOWER 


CONSTANTATTENUATION  
LINEARATTENUATION 
QUADRATICATTENUATION 
FALLOFFANGLE 
FALLOFFEXPONENT 
EMISSION 
EMISSIVE  
OPACITY 
REFRACTION 


Texture‐Related 
RENDERDEPTHSTENCILTARGET  
RENDERCOLORTARGET 
VIEWPORTPIXELSIZE 
DIFFUSEMAP 
SPECULARMAP 
NORMAL  
ENVIRONMENT 
ENVMAP  
ENVIRONMENTNORMAL 
Transforms   & Locations  
WORLD 
VIEW 
PROJECTION 

WORLDVIEW 
VIEWPROJECTION 
WORLDVIEWPROJECTION 
WORLDINVERSE 
VIEWINVERSE  
PROJECTIONINVERSE  
WORLDVIEWINVERSE  
VIEW  PROJECTIONINVERSE  

WORLDVIEWPROJECTIONINVERSE 
WORLDTRANSPOSE 
VIEWTRANSPOSE 
PROJECTIONTRANSPOSE 
WORLDVIEWTRANSPOSE 
VIEW  PROJECTIONTRANSPOSE 
WORLDVIEWPROJECTIONTRANSPOSE 
WORLDINVERSETRANSPOSE 
VIEWINVERSETRANSPOSE  
PROJECTIONINVERSETRANSPOSE  
WORLDVIEWINVERSETRANSPOSE  
VIEW  PROJECTIONINVERSETRANSPOSE  
WORLDVIEWPROJECTIONINVERSETRANSPOSE 
TRANSFORM 
LIGHTPOSITION  


*/
#define RESOURCE_TYPE_MATERIALSHADER 11

class WPENGIN_API CMaterialShader:public CGraphicsFileResource  
{
public:
	CMaterialShader(const TCHAR * resName,const TCHAR * szFilePath, D3DPOOL pool=D3DPOOL_DEFAULT);	

	virtual HRESULT   OnD3D9CreateDevice(IDirect3DDevice9* pd3dDevice);
	virtual  void  OnD3D9DestroyDevice();
	virtual  void OnD3D9LostDevice();
	virtual HRESULT   OnD3D9ResetDevice( IDirect3DDevice9* pd3dDevice);
	LPD3DXEFFECT GetEffect();

	void SetWorldMatrix(const D3DXMATRIX & matWorld);
	void SetViewMatrix(const D3DXMATRIX & matView);
	void SetProjMatrix(const D3DXMATRIX & matProj);
	void SetWorldViewMatrix(const D3DXMATRIX & matWorldView);
	void SetWorldViewProjMatrix (const D3DXMATRIX & matWorldViewProj);
	void SetViewProjMatrix (const D3DXMATRIX & matViewProj);
	void SetWorldViewInverseMatrix (const D3DXMATRIX & matWorldViewInverse);
	void SetViewInverseMatrix (const D3DXMATRIX & matViewInverse);
	void SetLightDir(const D3DXVECTOR4 & vecLightDir);
	void SetAmbientColor(const D3DXCOLOR & AmbientColor);
	void SetDiffuseColor(const D3DXCOLOR & DiffuseColo);
	void SetSpecularColor(const D3DXCOLOR & SpecularColor);
	void SetSpecularPower(const  float & shininess);
	void SetDiffseTexture(LPDIRECT3DTEXTURE9 pDiffuseTexture);
	void SetNormalTexture(LPDIRECT3DTEXTURE9 pNormalTexture);
	void SetHeightTexture(LPDIRECT3DTEXTURE9 pHeightTexutre);
	void SetCubeTexure(LPDIRECT3DCUBETEXTURE9 pCubeTexture);
	void Render( LPCSTR tn,int n,...);
	void Render( IRenderable * pRenderable);
	void  Render( int n,...);
	~CMaterialShader(void);
private:

	LPD3DXEFFECT m_pEffect;
	D3DXHANDLE    
		m_hTechnique,
		//Matrix

		m_world,// World
		m_view,
		m_proj,	
		m_worldView,
		m_worldViewProj,//WorldViewProjection
		m_viewProj,

		m_worldViewInverse,
		m_viewInverse, //ViewInverse

		//D3DXVECTOR4

		m_lightDir,
		m_ambientColor,
		m_diffuseColor,
		m_specularColor,

		//float
		m_shininess,

		//Texture
		m_diffuseTexture,
		m_normalTexture,
		m_heightTexture,
		m_cubeTexture;


	D3DXMATRIX 
		m_lastWorld,
		m_lastView,
		m_lastProj,
		m_lastWorldView,
		m_lastWorldViewProj,
		m_lastViewProj,
		m_lastWorldViewInverse,
		m_lastViewInverse;


	D3DXVECTOR4 m_lastLightDir;
	float   m_lastShininess;

	D3DXCOLOR m_lastAmbientColor,m_lastDiffuseColor,m_lastSpecularColor;
	LPDIRECT3DTEXTURE9 m_lastDiffseTexture,m_lastNormalTexture,m_lastHeightTexture;

	LPDIRECT3DCUBETEXTURE9  m_lastCubeTexture;
	//重置参数
	void ResetParameters();

	//设置参数的句柄
	void InitParametersHandle();





#pragma region UnitTest
#ifdef _DEBUG

	//创建一个 简单的窗口 并  调用相应的函数
	class GameUnitTest :  public ITerminate ,public IInitialize,public IRenderable ,public IUpdateable
	{
		void  OnTerminate( void );
		HRESULT  OnInitialize( void );
		virtual void    OnD3D9FrameRender( float fElapsedTime );

		virtual void OnUpdate( double fTime, float fElapsedTime );


		CMaterialShader * m_pMaterialShader;  
		CMeshInstance * pMeshInstance;
		CMesh   *  pMesh;
		CFileTexture * pDiffuseTextue,*pNormalTexture;
		ICamera  *m_Camera;
		CMeshInstance  * m_pPlane;

		CMaterial  * m_pMaterial;
		CSkyBox * m_pSkyBox;
		CRenderToTexture *  pRenderToTexture;
	};
public:
	static void UnitTest();


#endif // _DEBUG
#pragma endregion UnitTest

};

